<?php

function nate_error_handler($number, $message, $file, $line, $vars)
{
	$now = date("Y-m-d H:i:s");
	$log = "
		<p>$now: 
			Error: $number 
			Line: $line
			File: $file
			$message";

	$email = $log. "<pre>" . print_r($vars, 1) . "</pre>";

	$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	if ($number !== E_NOTICE) {
	
		//error_log($log, 3, 'other.txt');
		//error_log($email, 1, 'nratkinson2@eiu.edu', $headers);
		//echo("There was an error - it has been reported.  Sorry for any inconvenience.");
		//echo $email;
	}
}


set_error_handler('nate_error_handler');


$prefix = "mosscms";

session_name($prefix);
session_start();
date_default_timezone_set("America/Chicago");

$global_upload_dir = "/home/nate/domains/noleafclover.info/public_html/moss/uploads/";

$z_user		= $_SESSION[$prefix."_username"];
$z_admin	= $_SESSION[$prefix."_security"];
$z_groups	= $_SESSION[$prefix."_groups"];
$action_global = "include/admin_action.php";

$dept_name = "MOSS CMS";

$jquery_global = "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js'></script>";
$jquery_ui_global = "<script src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js'></script>
					<link type='text/css' href='../includes/jquery-ui.css' rel='stylesheet' />";
$ckeditor_global = "<script src='../includes/ckeditor/ckeditor.js'></script>";

/*********************************************************************
/*## MossCMS class extends mysqli */
class MossCMS extends mysqli {

	public function __construct(){
	
		$DBHost 		= "localhost";
		$DBUser			= "nate_nate";
		$DBPass			= "bigblue";
		$databaseName 	= "nate_moss";
		
		
		@parent::__construct($DBHost, $DBUser, $DBPass, $databaseName);
   		 // check if connect errno is set
   		 if ($this->connect_errno != 0){
      		// error has occoured, throw our DBConnectException with 
      		// error message and error code
      		throw new Exception($this->connect_error, $this->connect_errno);
		}


	
	}
	
	


	/*** GET NEWS LIST ******************************************************************
	/*## List news */
	public function news_list()
	{
	
		$query ="
			SELECT 
				*	
			FROM 
				content
			WHERE
				content_type = 1 
			ORDER BY content_date desc";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	
	/*** GET NEWS LIST FRONTEND ******************************************************************
	/*## List news */
	public function news_list_frontend($groups)
	{
	$groups = $this->convert_groups_to_array($groups);
		$query ="
			SELECT 
				*	
			FROM 
				content,
				content_groups
			WHERE
				content.content_id = content_groups.content_id
				AND	content.content_type IN(1) 
				AND content_groups.group_id IN ($groups)
			ORDER BY content_date desc";  
			
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** GET DOC/LINK LIST ******************************************************************
	/*## List docs/links */
	public function doc_list()
	{
	
		$query ="
			SELECT 
				*	
			FROM 
				content
			WHERE
				content_type IN(2,3) 
			ORDER BY content_date desc";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	
	/*** GET DOC/LINK LIST FRONTED ******************************************************************
	/*## List docs/links */
	public function doc_list_frontend($groups)
	{
	
	$groups = $this->convert_groups_to_array($groups);
		$query ="
			SELECT 
				*	
			FROM 
				content,
				content_groups
			WHERE
			content.content_id = content_groups.content_id
				AND	content.content_type IN(2,3) 
				AND content_groups.group_id IN ($groups)
			ORDER BY content_date desc";  
			
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	
	/*** GET CONTENT ******************************************************************
	/*## Gets content info */
	public function get_content($content_id)
	{
		
		$query ="
			SELECT 
				*	
			FROM 
				content
			WHERE content_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $content_id);
				$stmt->execute(); 
				$meta = $stmt->result_metadata();
	
			    while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			   $stmt->fetch();
			      $x = array();
			      foreach( $row as $key => $val ) {
			         $results[$key] = $val;
			      }		   
			
			   return $results;
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}		


	/*** ADD CONTENT ******************************************************************
	/*## Adds content */
	public function content_add($type,$title,$image,$thumb,$date,$content,$loc)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			INSERT INTO content 
				(content_type,
				content_title,
				content_image,
				content_thumb,
				content_date,
				content_content,
				content_loc)	
			VALUES
				(?,?,?,?,?,?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("issssss",$type, $title,$image,$thumb,$date,$content,$loc);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** EDIT CONTENT ******************************************************************
	/*## edit content */
	public function content_edit($id,$type,$title,$image,$thumb,$date,$content,$loc)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			UPDATE content SET 
				content_type = ?,
				content_title = ?,
				content_image = ?,
				content_thumb = ?,
				content_date = ?,
				content_content = ?,
				content_loc = ?	
			WHERE
				content_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("issssssi",$type, $title,$image,$thumb,$date,$content,$loc,$id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	
	
		
	/*** GET CONTENT GROUPS ******************************************************************
	/*## Gets groups that have access to content */
	public function get_content_groups($content_id)
	{
	
		$query ="
			SELECT 
				group_id	
			FROM 
				content_groups
			WHERE
				content_id = ?";  
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $content_id);
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** ADD CONTENT GROUP ******************************************************************
	/*## Adds group access to content */
	public function content_group_add($content,$group)
	{

		$query ="
			INSERT INTO content_groups 
				(content_id,
				group_id)	
			VALUES
				(?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ii",$content,$group);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** REMOVE CONTENT GROUP ******************************************************************
	/*## removes content_groups access to content */
	public function content_group_remove($content)
	{

		$query ="
			DELETE FROM 
				content_groups 
			WHERE
				content_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i",$content);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}

	/*** Convert Groups Array to Array for AQL ******************************************************************
	/*## removes content_groups access to content */
	public function convert_groups_to_array($groups)
	{

		for($x = 0; $x < sizeof($groups); $x++){
		$group_id = $this->group_info_by_name($groups[$x]);
			if($x == 0){$groups_final = $group_id;}
			else{
				$groups_final .= ",".$group_id;
			}
		}
		
	return $groups_final;	
	}
	
	
	
	/*** GET GROUPS LIST ******************************************************************
	/*## List groups */
	public function groups_list()
	{
	
		$query ="
			SELECT 
				*	
			FROM 
				groups
			ORDER BY group_name asc";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** ADD GROUP ******************************************************************
	/*## Adds group */
	public function groups_add($title)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			INSERT INTO groups 
				(group_name)	
			VALUES
				(?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("s",$title);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** GET GROUP ******************************************************************
	/*## Gets specific GROUP */
	public function group_info_by_name($group)
	{
		$query ="
			SELECT 
				group_id	
			FROM 
				groups
			WHERE group_name = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("s", $group);
				$stmt->execute(); 
				
				$stmt->bind_result($group_id);
				$stmt->fetch();			

			   return $group_id;
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}	


	
	
	
	/*** GET USER LEVELS LIST ******************************************************************
	/*## List hall names and id's */
	public function get_user_levels()
	{
	
		$query ="
			SELECT 
				user_level_id,
				user_level_name	
			FROM 
				user_levels 
			ORDER BY user_level_id";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
				$stmt->bind_result($user_level_id, $user_level_name);
					
					
      			while ($stmt->fetch()) {
      				$x = array();
      					$x[user_level_id] = $user_level_id;
      					$x[user_level_name] = $user_level_name;
					$results[] = $x;
			   }
			
			   return $results;
			
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	
	/*** GET USER LIST ******************************************************************
	/*## List notes */
	public function get_user_list()
	{
	
		$query ="
			SELECT 
				users.username, 
				users.user_fullname,
				user_levels.user_level_name	
			FROM 
				users, user_levels
			WHERE
				users.user_level_id = user_levels.user_level_id 
			ORDER BY user_fullname";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** GET USER ******************************************************************
	/*## Gets specific note */
	public function get_user_info($user_name)
	{
		
		$query ="
			SELECT 
				*	
			FROM 
				users
			WHERE username = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("s", $user_name);
				$stmt->execute(); 
				$meta = $stmt->result_metadata();
	
			    while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			   $stmt->fetch();
			      $x = array();
			      foreach( $row as $key => $val ) {
			         $results[$key] = $val;
			      }		   
			
			   return $results;
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	

	/*** ADD USER ******************************************************************
	/*## Adds a new user */
	public function user_add($netid, $fullname, $level)
	{
		$query ="
			INSERT INTO users 
				(username,
				user_fullname,
				user_level_id)	
			VALUES
				(?,?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ssi", $netid, $fullname, $level);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** EDIT USER ******************************************************************
	/*## Updates user */
	public function user_edit($netid, $fullname, $level)
	{
		$query ="
			UPDATE users SET
				user_fullname=?,
				user_level_id=?
			WHERE
				username=?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("sis", $fullname, $level, $netid);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** REMOVE USER ******************************************************************
	/*## Updates user */
	public function user_remove($username)
	{
		$query ="
			DELETE FROM
				users
			WHERE
				username=?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("s", $username);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** LOGIN ******************************************************************
	/*## LOGS USER INTO APP */
	public function login($username, $pass)
	{
	
		$login_status = 1;
		$query ="
			SELECT 
				username, 
				user_level_id 
			FROM 
				users 
			WHERE 
				username = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("s", $username);
				$stmt->execute(); 
				$stmt->store_result(); 
			
				if($stmt->num_rows == 1){
					$stmt->bind_result($username, $admin);
					$stmt->fetch();
					$namegood = "yes";
					$security = $admin;
					$login_status = 0;
				}
				$stmt->close();
			}
			include "includes/adLDAP.php";
      
			
			$login = array($login_status, $security);
			return $login;
			
	}
	
	/*** LOG LOGINS ******************************************************************
	/*## Logs user logins  */
	public function logins_log($username)
	{
		$agent	= $_SERVER['HTTP_USER_AGENT'];
		$ip		= $_SERVER['REMOTE_ADDR'];
		$refer	= $_SERVER['HTTP_REFERER'];
		$query ="
			INSERT INTO logins 
				(username,
				login_ip,
				login_browser,
				login_refer)	
			VALUES
				(?,?,?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ssss", $username, $ip, $agent, $refer);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** PRUNE LOGINS ******************************************************************
	/*## Prunes Logins  */
	public function logins_prune()
	{
		$today = date('m/d/Y');
		$day	= date('d');
		$month	= date('m');
		$year 	= date('Y');

			$sdate = date('Y-m-d', mktime(0, 0, 0, date("$month") , date("$day"), date("$year")));
			$fdate = date('Y-m-d', mktime(0, 0, 0, date("$month") , date("$day")-50, date("$year")));
		$query ="
			DELETE FROM logins 
				WHERE login_timestamp < ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("s", $fdate);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	
	/*** VIEW LOGINS ******************************************************************
	/*## QUERIES Logins  */
	public function logins_list()
	{
		$query ="
			SELECT 
				*	
			FROM 
				logins
			ORDER BY login_timestamp desc"; 
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
				$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;
			   	$stmt->close();
			   }//END PREPARE 		
	}

	/*** LOG ACTIONS ******************************************************************
	/*## Logs Action  */
	public function admin_action($username, $action)
	{
		$page	= $_SERVER['REQUEST_URI'];
		$agent	= $_SERVER['HTTP_USER_AGENT'];
		$ip		= $_SERVER['REMOTE_ADDR'];
		$refer	= $_SERVER['HTTP_REFERER'];
		$query ="
			INSERT INTO actions 
				(username,
				action_desc,
				action_page,
				action_ip,
				action_browser,
				action_refer)	
			VALUES
				(?,?,?,?,?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ssssss", $username, $action, $page, $ip, $agent, $refer);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** VIEW ACTIONS ******************************************************************
	/*## QUERIES Action  */
	public function admin_action_list()
	{
		$query ="
			SELECT 
				*	
			FROM 
				actions
			ORDER BY action_timestamp desc"; 
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
				$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;
			   	$stmt->close();
			   }//END PREPARE 		
	}
	
	/*** PRUNE ACTIONS ******************************************************************
	/*## Prunes Action  */
	public function admin_action_prune()
	{
		$today = date('m/d/Y');
		$day	= date('d');
		$month	= date('m');
		$year 	= date('Y');

			$sdate = date('Y-m-d', mktime(0, 0, 0, date("$month") , date("$day"), date("$year")));
			$fdate = date('Y-m-d', mktime(0, 0, 0, date("$month") , date("$day")-50, date("$year")));
		$query ="
			DELETE FROM actions 
				WHERE action_timestamp < ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("s", $fdate);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	










/*** REMOVE CONTENT ******************************************************************
	/*## removes content */
	public function content_remove($id)
	{
		$query ="
			DELETE FROM
				content
			WHERE
				content_id=?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
//PAGES
	/*** GET PAGES LIST ******************************************************************
	/*## List pages */
	public function pages_list()
	{
	
		$query ="
			SELECT 
				*	
			FROM 
				pages
			ORDER BY page_name asc";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** ADD PAGE ******************************************************************
	/*## Adds page */
	public function pages_add($title,$content,$active)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			INSERT INTO pages 
				(page_name,
				page_content,
				page_active)	
			VALUES
				(?,?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ssi",$title,$content,$active);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** GET PAGE ******************************************************************
	/*## Gets specific PAGE */
	public function pages_info($id)
	{
		$query ="
			SELECT 
				*	
			FROM 
				pages
			WHERE page_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute(); 
				$meta = $stmt->result_metadata();
	
			    while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			   $stmt->fetch();
			      $x = array();
			      foreach( $row as $key => $val ) {
			         $results[$key] = $val;
			      }		   
			
			   return $results;
			   	$stmt->close();
			   }//END PREPARE 

	
	
	}	

	/*** EDIT PAGES ******************************************************************
	/*## edit pages */
	public function pages_edit($id,$title,$content,$active)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			UPDATE pages SET 
				page_name = ?,
				page_content = ?,
				page_active = ?
			WHERE
				page_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ssii",$title,$content,$active,$id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** REMOVE PAGE ******************************************************************
	/*## removes page */
	public function pages_remove($id)
	{
		$query ="
			DELETE FROM
				pages
			WHERE
				page_id=?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	

//ANNOUNCEMENTS
	/*** GET ANNOUNCEMENTS LIST ******************************************************************
	/*## List pages */
	public function announce_list()
	{
	
		$query ="
			SELECT 
				*	
			FROM 
				site_updates
			ORDER BY update_timestamp asc";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** ADD ANNOUNCEMENT ******************************************************************
	/*## Adds announcement */
	public function announce_add($content,$user)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			INSERT INTO site_updates 
				(update_text,
				username)	
			VALUES
				(?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ss",$content,$user);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** GET ANNOUNCEMENT ******************************************************************
	/*## Gets specific PAGE */
	public function announce_info($id)
	{
		$query ="
			SELECT 
				*	
			FROM 
				site_updates
			WHERE update_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute(); 
				$meta = $stmt->result_metadata();
	
			    while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			   $stmt->fetch();
			      $x = array();
			      foreach( $row as $key => $val ) {
			         $results[$key] = $val;
			      }		   
			
			   return $results;
			   	$stmt->close();
			   }//END PREPARE 

	
	
	}	

	/*** EDIT ANNOUNCMENT ******************************************************************
	/*## edit announcement */
	public function announce_edit($id,$content)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			UPDATE site_updates SET 
				update_text = ?
			WHERE
				update_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("si",$content,$id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** REMOVE ANNOUNCMENT ******************************************************************
	/*## removes announcment */
	public function announce_remove($id)
	{
		$query ="
			DELETE FROM
				site_updates
			WHERE
				update_id=?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
}
	



//MENU
	/*** GET MENUS LIST ******************************************************************
	/*## List menus */
	public function menu_list()
	{
	
		$query ="
			SELECT 
				*	
			FROM 
				menu
			ORDER BY menu_order asc";  
			if ($stmt = parent::prepare($query)){
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** GET MENU ******************************************************************
	/*## Gets specific MENU */
	public function menu_info($id)
	{
		$query ="
			SELECT 
				*	
			FROM 
				menu
			WHERE menu_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute(); 
				$meta = $stmt->result_metadata();
	
			    while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			   $stmt->fetch();
			      $x = array();
			      foreach( $row as $key => $val ) {
			         $results[$key] = $val;
			      }		   
			
			   return $results;
			   	$stmt->close();
			   }//END PREPARE 

	
	
	}	
	
	/*** EDIT MENU ******************************************************************
	/*## edit menu */
	public function menu_edit($id,$name,$active)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			UPDATE menu SET 
				menu_name = ?,
				menu_active=?
			WHERE
				menu_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("sii",$name,$active,$id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** GET LINK LIST FOR MENU ******************************************************************
	/*## List links for menu */
	public function menu_link_list($menu)
	{
	
		$query ="
			SELECT 
				*	
			FROM 
				menu_links
			WHERE
				menulink_menu_id = ?
			ORDER BY menulink_order asc";  
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i",$menu);
				$stmt->execute(); 
					$meta = $stmt->result_metadata();
	
			   while ( $field = $meta->fetch_field() ) {
			     $parameters[] = &$row[$field->name];
			   }
			
			   call_user_func_array(array($stmt, 'bind_result'), $parameters);
			
			      while ( $stmt->fetch() ) {
				      $x = array();
				      foreach( $row as $key => $val ) {
				         $x[$key] = $val;
				      }
				      $results[] = $x;
				   }	   
			
			   return $results;	
					
			   	$stmt->close();
			   }//END PREPARE 
	
	
	}
	
	/*** ADD MENU LINK******************************************************************
	/*## Adds link to  menu */
	public function menu_link_add($menu_id,$name,$url,$target)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			INSERT INTO menu_links 
				(menulink_menu_id,
				menulink_link_name,
				menulink_link_url,
				menulink_target)	
			VALUES
				(?,?,?,?)"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("iss",$menu_id,$name,$url,$target);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	

	
	/*** UPDATE MENU LINK ORDER******************************************************************
	/*## Adds link to  menu */
	public function menu_link_order($menu,$link_id,$x)
	{
		$now = date("Y-m-d H:i:s");
		$query ="
			UPDATE menu_links SET 
				menulink_order = ?
			WHERE
				menulink_id = ?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("ii",$x,$link_id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
	}
	
	/*** REMOVE MENU LINK ******************************************************************
	/*## removes link */
	public function menu_link_remove($id)
	{
		$query ="
			DELETE FROM
				menu_links
			WHERE
				menulink_id=?"; 
			if ($stmt = parent::prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute(); 

			   	$stmt->close();
			   }//END PREPARE 
		
}


}//END CLASS
